import { Link as RouterLink } from 'react-router-dom';
import PropTypes from 'prop-types';

const Link = ({ children, onClick, disabled, to, href, ...props }) => {
  const handleClick = (e) => {
    if (disabled) {
      e.preventDefault();
      return;
    }
    if (onClick) {
      onClick(e);
    }
  };

  const commonProps = {
    onClick: handleClick,
    disabled: disabled,
    ...props
  };

  // 如果是禁用状态，使用 span 标签
  if (disabled) {
    return (
      <a {...commonProps}>
        {children}
      </a>
    );
  }

  // 如果有 to 属性，使用 RouterLink
  if (to) {
    return (
      <RouterLink to={to} {...commonProps}>
        {children}
      </RouterLink>
    );
  }

  // 如果有 href 属性或没有 to 属性，使用 a 标签
  return (
    <a href={href} {...commonProps}>
      {children}
    </a>
  );
};

Link.propTypes = {
  children: PropTypes.node,
  onClick: PropTypes.func,
  disabled: PropTypes.bool,
  className: PropTypes.string,
  to: PropTypes.oneOfType([
    PropTypes.string,
    PropTypes.object
  ]),
  href: PropTypes.string
};


export default Link; 